/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-09-02
 * Time: 23:06
 */
//模拟实现线程池
public class MyThreadPool {
    private Thread[] arr=null;                   //线程数组
    private MyBlocKing queue=new MyBlocKing();   //阻塞队列
    MyThreadPool(){
        arr=new Thread[3];
        for(int i=0;i<3;i++){
            int j=i;
            arr[i]=new Thread(()->{
                try {
                    while(true){
                        System.out.println("线程"+j+"执行任务:");
                        queue.take().run();
                    }
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }
            });
            arr[i].start();
        }
    }
    MyThreadPool(int n){
        arr=new Thread[n];
        for(int i=0;i<n;i++){
            arr[i]=new Thread(()->{
                try {
                    while(true){
                        queue.take().run();
                    }
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }
            });
            arr[i].start();
        }
    }
    public void submit(Runnable run){          //往线程池中放任务
        try{
            queue.put(run);
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
